<template>
  <div>
    <!-- <el-button class="el-icon-upload" type="success">导入</el-button> -->
    <el-button class="el-icon-download" type="success" @click="exportExcel()">导出</el-button>
  </div>
</template>
<script>
export default {
  name: "importandexport",
  data() {
    return {
      extp: this.exportExceltype,
    };
  },
  props: ["exportExceltype"],
  methods: {
    exportExcel() {
      const extpurl = ""; //导出请求地址
      if (this.extp == "user") {
        this.extpurl = "/exportUser";
      } else if (this.extp == "role") {
        this.extpurl = "/exportRole";
      }

      this.$axios
        .get(this.extpurl, { responseType: "blob" })
        .then((res) => {
          const blob = new Blob([res.data], {
            type: "application/vnd.ms-excel;charset=utf-8",
          });
          const href = window.URL.createObjectURL(blob); // 创建下载的链接
          const downloadElement = document.createElement("a");
          downloadElement.href = href;
          downloadElement.download = decodeURI(res.headers["filename"]);
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch((fail) => {
          console.error(fail);
        });
    },
  },
};
</script>
<style scoped>
</style>